<!DOCTYPE html>
<html lang="zh">
    <head>
        <meta charset="utf-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>新增/修改流程</title>
        <style>
           div,th,td {
                font-size: 12px;
           }
           td a {
               margin: 0px 2px;
           }
        </style>
    </head>
    <body>
        <div id="content">
            <div id="currentUser" style="color:green;font-weight:bold;font-size:14px;"></div>
            <div><a id="backTo" href='#'>返回</a></div>

            <div>&nbsp;</div>
            <div id="info">新增/修改流程</div>

            <div>&nbsp;</div>
            <div>
                <form id="flowForm">
                    ID: <span id="flowId"></span><br/>
                    工单类型(order_type)<font color="red">*</font>: <input type="text" id="orderType"><span style="color:red;">全局唯一，编辑状态中请不要修改此字段的值,流程表单、流程节点设计都是以此字段的值关联的</span><br/>
                    工单名称(order_name)<font color="red">*</font>: <input type="text" id="orderName"><br>
                    工单号前缀(order_id_prex)<font color="red">*</font>: <input type="text" id="orderIdPrex">工单号前缀，一般为大写字段，如请假的工单号前缀值为：QJ<br>
                    备注(remark): <input type="text" id="remark"><br>
                    <input type="button" onclick="addOrEdit();" value="保存">
                </form>
            </div>

        </div>
        <script src="js/config.js"></script>
        <script src="js/jquery.min.js"></script>
        <script type="text/javascript">
            var contextPath = getContextPath();
            var userId = getQueryVariable("userId");
            var id = getQueryVariable("id");

            //返回按钮
            document.getElementById("backTo").href = contextPath + "/main.html?userId="+userId;

            $(function() {
                //模拟所有参与人员
                //lix：普通人员
                //zhaoyl：项目经理
                //liujh,linjh,huangjc：部门经理
                //xuw：总经理
                let arr = ['lix', 'zhaoyl', 'linjh', 'xuw'];
                if(!arr.includes(userId)){
                    alert("未合法的userId，请查看main.html里面的定义.");
                    return;
                }
                //设置当前用户信息：
                document.getElementById("currentUser").innerHTML = "你是："+userId;

                //如果id不为空，则加载数据库中的数据进行填充表单
                if(id!=""){
                    $.ajax({
                        url:  window.location.origin + contextPath + '/workFlowDef/getById',
                        data: JSON.stringify({
                            id: id,
                        }),
                        type: 'post',
                        contentType: 'application/json;charset=utf-8',
                        dataType: "json",
                        success: function (res) {
                            if(res){
                                var aRow = res;
                                document.getElementById("flowId").innerText = aRow.id;
                                document.getElementById("orderType").value = aRow.orderType;
                                document.getElementById("orderName").value = aRow.orderName;
                                document.getElementById("orderIdPrex").value = aRow.orderIdPrex;
                                document.getElementById("remark").value = aRow.remark;
                            }
                        },
                        error: function (result) {alert("流程加载失败，内部错误.");}
                    });   
                }

            });

            //addOrEdit
            function addOrEdit(){
                var id = document.getElementById("flowId").innerText;
                var orderType = document.getElementById("orderType").value;
                var orderName = document.getElementById("orderName").value;
                var orderIdPrex = document.getElementById("orderIdPrex").value;
                var remark = document.getElementById("remark").value;

                if(orderType === ""){
                    alert("工单类型(order_type)不能为空.");
                    return;
                }
                if(orderName === ""){
                    alert("工单名称(order_name)不能为空.");
                    return;
                }
                if(orderIdPrex === ""){
                    alert("工单号前缀(order_id_prex)不能为空.");
                    return;
                }

                if(id===""){

                    //新增
                    $.ajax({
                        url:  window.location.origin + contextPath + '/workFlowDef/insert',
                        data: JSON.stringify({
                            orderType: orderType,
                            orderName: orderName,
                            orderIdPrex: orderIdPrex,
                            remark: remark
                        }),
                        type: 'post',
                        contentType: 'application/json;charset=utf-8',
                        dataType: "json",
                        success: function (res) {
                            if(res){
                                alert("流程新增成功");
                                window.location.href = contextPath + "/main.html?userId="+userId;
                            }
                        },
                        error: function (result) {alert("流程新增失败，内部错误.");}
                    });                    
                }else{
                    //修改
                    $.ajax({
                        url:  window.location.origin + contextPath + '/workFlowDef/update',
                        data: JSON.stringify({
                            id: id,
                            orderType: orderType,
                            orderName: orderName,
                            orderIdPrex: orderIdPrex,
                            remark: remark
                        }),
                        type: 'post',
                        contentType: 'application/json;charset=utf-8',
                        dataType: "json",
                        success: function (res) {
                            if(res){
                                alert("流程修改成功");
                                window.location.href = contextPath + "/main.html?userId="+userId;
                            }
                        },
                        error: function (result) {alert("流程修改失败，内部错误.");}
                    });  
                }
            }

            //获取浏览器地址上的url参数
            function getQueryVariable(variable){
                var query = window.location.search.substring(1);
                var vars = query.split("&");
                for (var i=0;i<vars.length;i++) {
                    var pair = vars[i].split("=");
                    if(pair[0] == variable){return pair[1];}
                }
                return "";
            }

        </script>
    </body>
</html>